package repo

import (
	"context"
	"ebip/internel/domain"
	"ebip/internel/repository/dao"
	"ebip/internel/repository/dao/user_dao"
)

// @Description
// @Author 代码小学生王木木

type IAuthRepo interface {
	FindByUserName(ctx context.Context, username string) (domain.UserInfoDM, error)
}

func NewAuthRepo(dao user_dao.IUserDAO) IAuthRepo {
	return &authRepo{
		dao: dao,
	}
}

type authRepo struct {
	dao user_dao.IUserDAO
}

func (a *authRepo) FindByUserName(ctx context.Context, username string) (domain.UserInfoDM, error) {
	user, err := a.dao.FindByUserName(ctx, username)
	if err != nil {
		return domain.UserInfoDM{}, err
	}
	return a.toDomain(user), err
}

func (a *authRepo) toDomain(item dao.UserMode) domain.UserInfoDM {
	return domain.UserInfoDM{
		ID:          int64(item.ID),
		CreatedAt:   item.CreatedAt,
		UpdatedAt:   item.UpdatedAt,
		Username:    item.Username,
		Phone:       item.Phone,
		Email:       item.Email,
		Description: item.Description,
		Password:    item.Password,
		Nickname:    item.Nickname,
		Gender:      item.Gender,
		Birthday:    item.Birthday,
		IsAdmin:     item.IsAdmin,
		Avatar:      item.Avatar,
	}
}
